/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-08-27 15:38:23
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-17 16:55:54
 * @FilePath: /react18-vite-admin/src/router/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { type RouteObject } from 'react-router-dom';
import { HomeOutlined,  } from '@ant-design/icons';

import Layout from '@/layout/Layout';
import { usePermissionStore } from '@/store';
import LoginView from '@/views/LoginView/LoginView';
import Dashboard from '@/views/Dashboard/Dashboard';
import { systemRoutes } from './system';

// 静态路由
export const constantRoutes: AppRouteRecordRaw[] = [
    {
        path: '/login',
        element: <LoginView></LoginView>,
        name: 'LoginView',
        meta: { hidden: true, title: '登录页' }
    },
    {
        path: '*',
        element: <>404</>,
        name: 'NotFind',
        meta: { hidden: true }
    }
    // {
    //     path: '/401',
    //     name: 'NoPermission',
    //     component: () => import('@/views/error-page/401.vue'),
    //     meta: { hidden: true }
    // },
    // {
    //     path: '/iframe',
    //     name: 'Iframe',
    //     component: () => import('@/views/common/iframe/index.vue'),
    //     meta: { hidden: true }
    // },
    // {
    //     path: '/:catchAll(.*)',
    //     name: 'NotFind',
    //     component: () => import('@/views/error-page/404.vue'),
    //     meta: { hidden: true }
    // }
];

export const authRoutes: AppRouteRecordRaw[] = [
    {
        path: '/',
        name: 'Root',
        element: <Layout></Layout>,
        redirect: '/dashboard',
        meta: {},
        children: [
            {
                path: '/dashboard',
                name: 'DashboardView',
                element: <Dashboard></Dashboard>,
                meta: { title: '仪表盘', icon: <HomeOutlined />, affix: true }
            }
        ]
    },

    ...systemRoutes,
    // ...dataManageRoutes,
    // ...appManageRoutes
];

export const createReactRouterObjects = (list: AppRouteRecordRaw[]) => {
    return list.map((item) => {
        const routerObject: RouteObject = {
            path: item.path,
            id: item.name,
            loader: () => {
                return {
                    title: item.meta.title || ''
                };
            }
        };
        routerObject.element = item.element;
        routerObject.Component = item.Component;
        if (item.children && item.children.length > 0) {
            routerObject.children = createReactRouterObjects(item.children);
        }
        if (item.redirect && item.path) {
            const obj = JSON.parse(
                JSON.stringify(usePermissionStore.getState().redirectPaths)
            );
            obj[item.path] = item.redirect;
            usePermissionStore.setState({ redirectPaths: obj });
        }
        return routerObject;
    });
};
